<?php

class IndexController extends Application_Model_Controller
{

    public function init()
    {
        $this->view->headTitle('Главная');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.sortable.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/money.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/buy.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/idea.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/travel.js');
        $this->view->headScript()->appendFile($this->view->baseUrl()  . '/js/redactor/redactor.js');
        $this->view->headLink()->appendStylesheet($this->view->baseUrl() . '/js/redactor/css/redactor.css', 'screen');
        $this->tplPath = __DIR__ . '/../views/scripts/sidebars/';
    }

    public function indexAction()
    {
        $this->view->mainPage = true;

        //Финансы
        $mapperProfit = new Application_Model_ProfitMapper();
        $statsProfit = $mapperProfit->getStatsProfit();
        $this->view->statsProfit   = $statsProfit;
        $this->view->planeProfit = isset(Application_Model_ProfitMapper::$profitPlane[date('Y')]) ? Application_Model_ProfitMapper::$profitPlane[date('Y')] : false;
        $mapperDebts = new Application_Model_DebtMapper();
        $this->view->debtsSum = $mapperDebts->getSumDebts();
        $this->view->tplMoney = $this->tplPath . 'money.phtml';

        //Покупки
        $mapperBuy = new Application_Model_Mapper(new Application_Model_DbTable_Buy(), new Application_Model_Buy());
        $this->view->topGoods = $mapperBuy->getCollection('buy_status > 0', 'buy_status DESC', array('count' => 10, 'start' => 0));
        $this->view->tplBuy = $this->tplPath . 'buy.phtml';

        //Планы

        $mapperIdea = new Application_Model_IdeaMapper();
        $this->view->tasks = $mapperIdea->getCollection(array('task_limit' => Application_Model_Task::CURRENT, 'task_status' => Application_Model_Task::STATUS_OPEN), 'task_priority DESC');
        $this->view->tplIdea = $this->tplPath . 'current-idea.phtml';
        //Планы на год
        $this->view->tasksYear = $mapperIdea->getCollection(array('task_limit' => Application_Model_Task::ON_YEAR, 'task_status' => Application_Model_Task::STATUS_OPEN), 'task_priority DESC');

        //Учеба
        $mapperStudy = new Application_Model_StudyMapper(new Application_Model_DbTable_Terms(), new Application_Model_Term());
        $this->view->termsInAction = $mapperStudy->getCollection(array('term_in_action' => 1));
        $this->view->tplStudy = $this->tplPath . 'study.phtml';


        //Путешествия
        $mapperTravels = new Application_Model_Mapper(new Application_Model_DbTable_Travels(), new Application_Model_Travel());
        $this->view->travels = $mapperTravels->getCollection(array('status' => Application_Model_Travel::ACTIVE), 'id DESC');
        $this->view->tplTravels = $this->tplPath . 'travel.phtml';

    }

    public function profilerAction()
    {
        $isEnable = 'on' === $this->_getParam('mode');
        $session = new Zend_Session_Namespace('prf');

        if ($isEnable) {
            $session->val = 1;
        } else {
            $session->val = 0;
        }

        $this->_redirect($this->_getParam('url'));
    }

    public function notesAction()
    {
        $tagId = $this->_getParam("id");

        if ($tagId) {
            $this->view->notes = (new Application_Model_Mapper(new Application_Model_DbTable_Notes(), new Application_Model_Note()))->getCollection(array("note_type" => $tagId));
        } else {
            $this->view->notes = (new Application_Model_Mapper(new Application_Model_DbTable_Notes(), new Application_Model_Note()))->getCollection();
        }
    }

}

